package org.example.jdbc_study.service;

import org.example.jdbc_study.domain.dto.MyUserDto;
import org.example.jdbc_study.domain.po.MyUser;
import org.example.jdbc_study.domain.vo.MyUserVo;
import org.example.jdbc_study.mapper.MyUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service
 *
 * @author 欧阳劲驰
 * @since 0.0.1
 */
@Service
public class MyUserService {
    @Autowired
    MyUserMapper myUserMapper;

    /**
     * 添加用户
     *
     * @param myUserVo 用户Vo
     * @return 添加状态
     */
    public Boolean addMyUser(MyUserVo myUserVo) {
        MyUser myUser = new MyUser();
        myUser.setUsername(myUserVo.getUsername());
        myUser.setPassword(myUserVo.getPassword());
        return myUserMapper.insert(myUser) > 0;
    }

    /**
     * 获取用户
     *
     * @return 用户集合
     */
    public List<MyUserDto> getMyUser() {
        return myUserMapper.selectAll();
    }

    /**
     * 修改用户
     *
     * @param myUserVo 用户Vo
     * @return 用户集合
     */
    public Boolean editMyUser(MyUserVo myUserVo) {
        return myUserMapper.updateUsernameAndPasswordById(
                myUserVo.getUsername(), myUserVo.getPassword(),
                myUserVo.getId()
        ) > 0;
    }

    /**
     * 删除用户
     *
     * @param id id
     * @return 用户集合
     */
    public Boolean delMyUser(Long id) {
        return myUserMapper.deleteById(id) > 0;
    }
}
